(* Copyright (C) 2010 School of Software Engineering, 
 * University of Science and Technology of China.
 *
 * This code is released under a BSD-style license.
 * See the file LICENSE for details.
 *)

structure Trace:> TRACE =
struct

val map: {name: string, flag: bool} list ref = ref []

fun enter x = map := {name = x, flag = true}::(!map)

fun lookup name =
    let val rec doit =
         fn [] => false
          | {name = x, flag}::xs =>
            if x=name
            then true
            else doit xs
    in  doit (!map)
    end
    
fun trace (name, f, la, lb) x =
    if lookup name
    then let val result = f x
             val _ = File.output (String.concat [name, ".", "before"],
                                 Layout.toString (la x))
             val _ = File.output (String.concat [name, ".", "after"],
                                  Layout.toString (lb result))
         in  result
         end
    else f x

end
